/** Clinet Application that make remote method invocation

   The client application
	- uses the classes, interface and exceptions defined in java.rmi package.
	- defines main() and other method and data members.
	- calls Naming's static function lookup( String)) to create a remote server
	  object by passing the name of a remote object to lookup(). The remote object
	  name has format
		//hostname:port/remoteObjectName
	  The port is optional. If missing, a default port is used.
	- Makes remote method invocations to get services from remote server object.

   Compile and run the client appliction.

   Notice that stub class generated by rmic compiler must be accessible by client application.
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.rmi.*;   
import java.util.*;   

public class ChevalDBClient 
{
   String sql = "Select * From AddressBook";
   public ChevalDBClient ( String ip ) 
   {
      super();
      getData( ip ) ;
   }

   // obtain weather information from TemperatureServerImpl
   // remote object
   private void getData(String ip)
   {         
      try {
         // name of remote server object bound to rmi registry
         String serverObjectName = "//" + ip + "/ChevalDBServer"; 

         // look remote object in rmiregistry
	 System.err.println("Looking for remote server ...\n");
         ChevalDBServer dbObj = ( ChevalDBServer ) Naming.lookup( serverObjectName );
	 showData(dbObj);
      }
      catch ( java.rmi.ConnectException ce ) {
         System.err.println( "Connection to server failed. " +
            "Server may be temporarily unavailable." ); }
      catch ( RemoteException e ) {
		System.err.println("Error in instantiating remote object\n" +
		e.toString() ); }
      catch ( Exception e ) { e.printStackTrace(); System.exit( 1 ); }      
   }

   private void showData( ChevalDBServer dbObj ) {
	try {
		byte s[] = new byte[100];
		System.out.print("Enter a SQL statement: ");
		try { System.in.read(s); }
		catch (IOException e) { System.err.println(e.toString());
			System.exit(1); }
		Vector rows = dbObj.getResultSet(new String(s));
		int rowCount = rows.size();
		DBRow lenRow , row;
		lenRow = (DBRow) rows.remove(0);
		int cols = lenRow.getColumnCount();
		Enumeration e = rows.elements();
		while ( e.hasMoreElements() ) {
			row = (DBRow) e.nextElement();
			for ( int i = 0; i < cols; i ++ )
				System.out.print(ScreenIO.expandString(row.getColumn(i) + " ",
					Integer.parseInt(lenRow.getColumn(i))));
			System.out.println();
		}
	} catch (RemoteException e) { System.err.println(e.toString()); System.exit(1); }
   }

   public static void main( String args[] )
   {
      ChevalDBClient gt = null;

      // if no sever IP address or host name specified,
      // use "localhost"; otherwise use specified host
      if ( args.length == 0 )
         gt = new ChevalDBClient( "localhost" );
      else
         gt = new ChevalDBClient( args[ 0 ] );
   }
}
